package Interpreter.Function.Time;

import Interpreter.Function.Function;
import Interpreter.InterpreterVisitor;
import Interpreter.Observation;
import ast.AST;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author David
 */
public class AddDaysFunction extends Function {

    @Override
    public Object execute(ArrayList args) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd kk:mm:ss z yyyy");

        Object o = ((AST) args.get(0)).accept(new InterpreterVisitor());
        String numDays = ((AST) args.get(1)).accept(new InterpreterVisitor()).toString();
        String date = "";

        try {
            if (o.getClass() == Class.forName("Interpreter.Observation")) {

                Date d = ((Observation) o).getDate();
                date = d.toString();
            } else {
                date = o.toString();
            }
            try {
                Calendar cal = Calendar.getInstance();
                cal.setTime(dateFormat.parse(date));
                cal.add(Calendar.DATE, Integer.parseInt(numDays));
                return cal.getTime();
            } catch (Exception e) {
                System.out.println(e.toString());
            }
        } catch (ClassNotFoundException ex) {
            System.out.println(ex.toString());
        }

        return null;
    }
}
